home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / popular.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  9KB  |  340 lines

  1. /*
  2.  *                              popular.c
  3.  *
  4.  * Rough popularity estimator for rooms.
  5.  */
  6.  
  7. #include "ctdl.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #include <proto/exec.h>
  15. #include <dos/dos.h>
  16. #include <pragmas/dos_pragmas.h>
  17. #include "exec/memory.h"
  18. #include "exec/ports.h"
  19. #include "exec/exec.h"
  20.  
  21. /*
  22.  *                              history
  23.  *
  24.  * 90Apr27 HAW  Version 2.3 - sorted output.
  25.  * 89Oct13 HAW  Version 2.2.
  26.  * 86May12 HAW  Version 2.1.
  27.  * 86Apr?? HAW  Version 2.0.
  28.  * 86Apr07 HAW  Version 1.1.
  29.  * 86Apr01 HAW  Created.
  30.  */
  31.  
  32. /*
  33.  *                              contents
  34.  *
  35.  *      crashout()              irrecoverable error
  36.  *      main()                  Main controller for this program
  37.  */
  38. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  39.  
  40. #define LINE \
  41. "------------------------------------------------------------------------------"
  42.  
  43. struct counts {
  44.     int  forgot, num;
  45.     int  messages;
  46.     long bytes;
  47. } *countTab;
  48.  
  49. int    messFlag = FALSE;
  50. extern MessageBuffer  msgBuf; /* The -sole- message buffer      */
  51. extern FILE *msgfl, *msgfl2;
  52. extern FILE *upfd;  /* file descriptor for the msg file */
  53. int PublicRoomCount = 0, ForgetCount = 0;
  54. int activeLogs = 0, activeForgots = 0;
  55.  
  56. extern rTable *roomTab; /* RAM index of rooms   */
  57. extern aRoom  roomBuf;  /* room buffer  */
  58. extern FILE     *roomfl;        /* file descriptor for rooms    */
  59. extern int      thisRoom;       /* room currently in roomBuf    */
  60. extern logBuffer logBuf;        /* Log buffer of a person       */
  61. extern int      thisLog;        /* entry currently in logBuf    */
  62. extern FILE     *logfl; /* log file descriptor  */
  63. extern CONFIG    cfg;   /* A buncha variables   */
  64.  
  65. void getCUdate(int *year, char **month, int *day, int *hours, int *minutes);
  66. void flags(int argc, char *argv[]);
  67. int mulchAcct(void);
  68. void doMessages(void);
  69. void display(void);
  70. void handle(void);
  71.  
  72. /*
  73.  * crashout()
  74.  *
  75.  * This handles the irrecoverable error.
  76.  */
  77. void crashout(str)
  78. char *str;
  79. {
  80.     exit(printf(str));
  81. }
  82.  
  83. /*
  84.  * display()
  85.  *
  86.  * This function displays the results.
  87.  */
  88. void display()
  89. {
  90.     int rover;
  91.     void ShowIt();
  92.     int year, day, hours, minutes;
  93.     char *month;
  94.     int numcmp();
  95.     static SListBase Sorted = { NULL, NULL, numcmp, NoFree, NULL };
  96.  
  97.  
  98.     for (rover = 0;  rover < MAXROOMS;  rover++)
  99.         if (roomTab[rover].rtflags.INUSE != 0 &&
  100.             roomTab[rover].rtflags.PUBLIC != 0) {
  101.             AddData(&Sorted, countTab + rover, NULL, FALSE);
  102.             countTab[rover].num = rover;
  103.         }
  104.  
  105.     getCUdate(&year, &month, &day, &hours, &minutes);
  106.     printf("%d%s%02d @ %d:%02d\n\n", year, month, day, hours, minutes);
  107.  
  108.     printf(
  109. "Out of a log of %d entries, %d are in use; %d (%d%%) have used <Z>Forget.\n\n",
  110.  cfg.MAXLOGTAB, activeLogs, activeForgots, (100* activeForgots) / activeLogs);
  111.  
  112.     printf("%48s%15s\n", "Total", "Forgotten");
  113.     printf("%-25s%-15s%-13s%s", "Room name", "# Forgotten", "Percentage",
  114.                                                         "Percentage");
  115.     if (messFlag) printf("%10s", "Messages");
  116.     printf("\n%s\n", LINE);
  117.  
  118.     RunList(&Sorted, ShowIt);
  119.  
  120.     ForgetCount /= PublicRoomCount;
  121.     printf("%s\n\n", LINE);
  122.     printf("There are %d public rooms, ", PublicRoomCount);
  123.     printf("an average %d forgetting them (%d%% and %d%%)\n\n",
  124.                                                         ForgetCount,
  125.     (activeLogs != 0) ? (100 * ForgetCount) / activeLogs : 0,
  126.     (activeForgots != 0) ? (100 * ForgetCount) / activeForgots : 0);
  127. }
  128.  
  129. /*
  130.  * numcmp()
  131.  *
  132.  * This function will compare two counts and return who's higher.  This is
  133.  * used in list sorting.
  134.  */
  135. int numcmp(struct counts *s, struct counts *d)
  136. {
  137.     return d->forgot - s->forgot;
  138. }
  139.  
  140. /*
  141.  * ShowIt()
  142.  *
  143.  * This will show a room with forgotten stats.  This is used in list handling.
  144.  */
  145. void ShowIt(struct counts *s)
  146. {
  147.     PublicRoomCount++;
  148.     ForgetCount += s->forgot;
  149.     printf("%-30s%-15d%-9d%3d", roomTab[s->num].rtname,
  150.                                                         s->forgot,
  151.    (activeLogs != 0) ? (100 * s->forgot) / activeLogs : 0,
  152.    (activeForgots != 0) ? (100 * s->forgot) / activeForgots : 0);
  153.     if (messFlag) printf("%11d (%ld)", s->messages,
  154.                 (s->messages == 0) ? 0 : s->bytes / (long) s->messages);
  155.     printf("\n");
  156. }
  157.  
  158. /*
  159.  * doMessages()
  160.  *
  161.  * This function loops thru the msg file until finished.  It accumulates
  162.  * statistics, etc.
  163.  */
  164. void doMessages()
  165. {
  166.     MSG_NUMBER msg, firstMessage;
  167.     MSG_NUMBER total;   /* For stat keeping. */
  168.     extern struct mBuf mFile1;
  169.  
  170.     fprintf(stderr, "Mulching...\n");
  171.     InitMsgBase();
  172.     startAt(msgfl, &mFile1, 0, 0);
  173.     getMessage(getMsgChar, FALSE, TRUE, TRUE);
  174.     msg = atol(msgBuf.mbId);
  175.     fprintf(stderr, "%ld\n", msg);
  176.     firstMessage = msg;
  177.     handle();
  178.     getMessage(getMsgChar, FALSE, TRUE, TRUE);
  179.     msg = atol(msgBuf.mbId);
  180.     total = 1;
  181.     while (msg != firstMessage) {
  182.         total++;
  183.         fprintf(stderr, "%ld\r", msg);
  184.         handle();
  185.         getMessage(getMsgChar, FALSE, TRUE, TRUE);
  186.         msg = atol(msgBuf.mbId);
  187.     }
  188. }
  189.  
  190. /*
  191.  * flags()
  192.  *
  193.  * This function analyzes the command line arguments.  Very primitive.
  194.  */
  195. void flags(argc, argv)
  196. int argc;
  197. char *argv[];
  198. {
  199.     int i;
  200.  
  201.     for (i = 0; i < MAXROOMS; i++) {
  202.         countTab[i].forgot = 0;
  203.         countTab[i].messages = 0;
  204.     }
  205.  
  206.     while (argc != 1) {
  207.         argc--;
  208.         if (strCmpU("-M", argv[argc]) == 0)
  209.             messFlag = TRUE;
  210.     }
  211. }
  212.  
  213. /*
  214.  * getCUdate()
  215.  *
  216.  * This function retrieves the system date and returns in the parameters.
  217.  */
  218. void getCUdate(year, month, day, hours, minutes)
  219. int *year, *day, *hours, *minutes;
  220. char **month;
  221. {
  222.     int m;
  223.     static char *monthTab[13] = {"", "Jan", "Feb", "Mar",
  224.                                 "Apr", "May", "Jun",
  225.                                 "Jul", "Aug", "Sep",
  226.                                 "Oct", "Nov", "Dec" };
  227.  
  228.     getUtilDate(year, &m, day, hours, minutes);
  229.     *month = monthTab[m];
  230.     *year -= 1900;
  231. }
  232.  
  233. /*
  234.  * handle()
  235.  *
  236.  * This function searches the room base for the current msg's room, increments
  237.  * the appropriate counters when found.
  238.  */
  239. void handle()
  240. {
  241.     int rover;
  242.     int MsgLen(void);
  243.  
  244.     for (rover = 0; rover < MAXROOMS; rover++)
  245.         if (strCmpU(roomTab[rover].rtname, msgBuf.mbroom) == 0) {
  246.             countTab[rover].messages++;
  247.             countTab[rover].bytes += MsgLen();
  248.             break;
  249.         }
  250. }
  251.  
  252. /*
  253.  * MsgLen()
  254.  *
  255.  * This function figures out the byte usage of the message.
  256.  */
  257. int MsgLen()
  258. {
  259.     return  (int) (     strLen(msgBuf.mbtext)  + strLen(msgBuf.mbauth) +
  260.                     strLen(msgBuf.mbdate)  + strLen(msgBuf.mbtime) +
  261.                     strLen(msgBuf.mbId)    + strLen(msgBuf.mboname) +
  262.                     strLen(msgBuf.mborig)  + strLen(msgBuf.mbroom) +
  263.                     strLen(msgBuf.mbsrcId) + strLen(msgBuf.mbto) +
  264.                     strLen(msgBuf.mbaddr)  + strLen(msgBuf.mbOther) );
  265. }
  266.  
  267. /*
  268.  * main()
  269.  *
  270.  * This is the main controller.
  271.  */
  272. int main(int, char **);
  273. int main(argc, argv)
  274. int argc;
  275. char *argv[];
  276. {
  277.     SYS_FILE temp;
  278.     int Index;
  279.  
  280.     cfg.weAre = UTILITY;
  281.     printf("\nCitadel Room Popularity Estimator %s\n%s\n", VERSION_NAME, COPYRIGHT);
  282.     fprintf(stderr, "Munching...\n");
  283.     printf("\n");
  284.     if (readSysTab(FALSE, TRUE)) {
  285.         initRoomBuf(&roomBuf);
  286.         countTab = (struct counts *) GetDynamic(MAXROOMS * sizeof *countTab);
  287.         flags(argc, argv);
  288.         mvToHomeDisk(&cfg.homeArea);
  289.         makeSysName(temp, "ctdlroom.sys", &cfg.roomArea);
  290.         openFile(temp, &roomfl);
  291.         makeSysName(temp, "ctdllog.sys", &cfg.logArea);
  292.         openFile(temp, &logfl);
  293.         initLogBuf(&logBuf);
  294.  
  295.         for (Index = 0; Index < cfg.MAXLOGTAB; Index++) {
  296.             getLog(&logBuf, Index);
  297.             if (logBuf.lbflags.L_INUSE) {
  298.                 activeLogs++;
  299.                 activeForgots += mulchAcct();
  300.             }
  301.             fprintf(stderr, "%d\r", Index);
  302.         }
  303.  
  304.         if (messFlag)
  305.             doMessages();
  306.  
  307.         display();
  308.     }
  309.     return 0;
  310. }
  311.  
  312. /*
  313.  * mulchAcct()
  314.  *
  315.  * This goes through all the rooms, checking them against the current acct
  316.  * for statistics gathering.
  317.  */
  318. int mulchAcct()
  319. {
  320.     int i, j, g, usedForget;
  321.  
  322.     usedForget = 0;
  323.     for (i = 0;  i < MAXROOMS;  i++) {
  324.         if (roomTab[i].rtflags.PUBLIC != 0) {
  325.             if ((logBuf.lbgen[i] >> GENSHIFT) != roomTab[i].rtgen)  {
  326.                 j = roomTab[i].rtgen - (logBuf.lbgen[i] >> GENSHIFT);
  327.                 if (j < 0)
  328.                     g = -j;
  329.                 else
  330.                     g = j;
  331.                 if (g == FORGET_OFFSET) {
  332.                     usedForget = 1;
  333.                     countTab[i].forgot++;
  334.                 }
  335.             }
  336.         }
  337.     }
  338.     return usedForget;
  339. }
  340.